<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/17
 * Time: 下午7:54
 */

namespace app\mall\logic;


use extend\Functions;
use extend\STATUS_CODE;

class Member
{
    /**
     * @purpose 获取代理商升级条件
     * @param int $invitation
     * @return array
     */
    public static function agentUpgradeConditions (string $invitation)
    {
        //获取代理商升级条件
       $config          = Configure::getConfigure('agent_upgrade');
       $config          = $config['data'];
       // 获取当前用户邀请的一级用户数量
        $member         = new \app\model\Member();
        $total    = $member
            ->where('invitation_from', $invitation)
            ->count(1);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'invitation' => $config['invitation'],
                'pay'   => $config['pay'],
                'invitation_num' => $total
            ]
        );
    }

    /**
     * @purpose 盟友数量
     * @param string $invitation
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function invitationTotal (string $invitation):array
    {
        $model          = new \app\model\Member();
        $invitations    = self::secondInvitation($invitation);
        $first_total    = $model->total(['invitation_from' => $invitation]);
        $second_total   = $model->total([['invitation_from', 'in', $invitations]]);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'first'     => $first_total,
                'second'    => $second_total
            ]
        );
    }
    /**
     * @purpose 获取一级盟友
     * @param string $invitation
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function invitationFirst(string $invitation, int $page = 1, int $size = 100): array
    {
        $model      = new \app\model\Member();
        $result     = $model
            ->where('invitation_from', $invitation)
            ->field('uuid,nickname,avatar,FROM_UNIXTIME(create_at, \'%Y-%m-%d %H:%i\') as register_date,FROM_UNIXTIME(last_login_time, \'%Y-%m-%d %H:%i\') as last_login_date')
            ->page($page, $size)
            ->order('create_at desc')
            ->select();
        $total      = $model->total(['invitation_from' => $invitation]);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'total' => $total,
                'list'  => $result
            ]
        );
    }

    /***
     * @purpose 获取当前用户下的二级盟友
     * @param string $invitation
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function invitationSecond(string $invitation, int $page = 1, int $size = 100):array
    {
        $model          = new \app\model\Member();
        $invitations    = self::secondInvitation($invitation);
        $result         = $model
            ->with('parent')
            ->where('invitation_from', 'in', $invitations)
            ->field('uuid,invitation,invitation_from,nickname,avatar,FROM_UNIXTIME(create_at, \'%Y-%m-%d %H:%i\') as register_date,FROM_UNIXTIME(last_login_time, \'%Y-%m-%d %H:%i\') as last_login_date')
            ->page($page, $size)
            ->order('create_at desc')
            ->select();
        $total      = $model->total([['invitation_from','in', $invitations]]);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'total' => $total,
                'list'  => $result
            ]
        );
    }
    /**
     * @purpose 获取所有一级盟友的邀请码
     * @param string $invitation
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private static function secondInvitation (string $invitation)
    {
        $model      = new \app\model\Member();
        $result     = $model
            ->where('invitation_from', $invitation)
            ->field('invitation')
            ->select();
        if ($result) {
            $result = $result->toArray();
        }
        $_result = [];
        foreach ($result as $v) {
            array_push($_result, $v['invitation']);
        }
        return $_result;
    }
    /**
     * @purpose 通过token获取用户信息
     * @param string $token
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function infoByToken (
        string $token):array
    {
        $model      = new \app\model\MemberLoginToken();
        $result     = $model
            ->with('userinfo')
            ->where('wechat_token', $token)
            ->field('uid')
            ->find();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }
}